/*
 *@本JS功能：学生虚机分配
 *@本js作者：杨锡君
 *@编写时间：2017年11月17日
 */
$(function() {
	var Alert1 = new Alert();
	var confirm1 = new Confirm();
    var loading1 = new Loading();
    loading1.init();
	var $departNames = $("#departNames");
	var $tableBox = $(".tableList");
	var $searchBtn = $(".searchBtn");
	var $mb = $(".mb");
	var th = "<tr>" +
		"<th class=\"col-lg-1\">序号</th>" +
		"<th class=\"col-lg-2\">班级名称</th>" +
		"<th class=\"col-lg-2\">院系</th>" +
		"<th class=\"col-lg-2\">专业</th>" +
		"<th class=\"col-lg-3\">分配类型</th>" +
		"<th class=\"col-lg-2\">操作</th>" +
		"</tr>";

	function tabelModel(opt, startRow) {
		var departmentName = opt.departmentName;
		var professionName = opt.professionName;
		if(departmentName == undefined){
            departmentName = "";
		}
        if(professionName == undefined){
            professionName = "";
        }
		return "<tr data-num=\"0\">" +
			"<td class=\"col-lg-1\">" + startRow + "</td>" +
			"<td class=\"col-lg-2\" title=" + opt.className + ">" + opt.className + "</td>" +
			"<td class=\"col-lg-2\" title=" + departmentName + ">" + departmentName + "</td>" +
			"<td class=\"col-lg-2\" title=" + professionName + ">" + professionName + "</td>" +
			"<td class=\"col-lg-3\" title=" + opt.templateType + ">" + opt.templateType + "</td>" +
			"<td class=\"col-lg-2\">" +
			"<a href=\"javascript:;\" title=\"分配虚机\"  data-classid=" + opt.classId + "  class=\"virAllocateBtn\">分配虚机</a>" +
			"<a class=\"virManaBtn\" data-name=" + opt.className + "  data-classid=" + opt.classId + "  title=\"虚机管理\"  href=\"javascript:;\">虚机详情</a>" +
			"</td>" +
			"</tr>";
	}

	function virConfigBox(opt) {
		var str = "<div class=\"popup\">" +
			"<div class=\"popup_box\">" +
			"<div class=\"popup_contenthead\">分配虚机<img class=\"popup_img\" src=\"pc/images/ico_alertDel.png\"></div>" +
			"<div class=\"popup_content\">" +
			"<div class=\"serverCountBox\">";
		var str1 = "";
		if(opt.serverCount.length != 0) {
			$.map(opt.serverCount, function(obj) {
				str1 += "<span class=\"fl\">" + obj.templateName + "：" + obj.serverCount + "</span>"
			});
		}
		var str2 = "<div class=\"clear\"></div></div>";
		var str3 = "";
		if(opt.template.length != 0) {
			$.map(opt.template, function(omg) {
				if(omg.isAvailable == 1) {
					if(omg.isAllocate == 1){
                        str3 += "<div data-isavailable=" + omg.isAvailable + " data-isallocate=" + omg.isAllocate + " class=\"configBox\">" +
                            "<div class=\"configTitle\">" + omg.templateName + " <input data-templateid=" + omg.templateId + "  value=\"1\"  name=\"selecteds\"  checked=\"checked\" type=\"checkbox\" class=\"oneCheck\"/></div>";

                    }else{
                        str3 += "<div data-isavailable=" + omg.isAvailable + " data-isallocate=" + omg.isAllocate + " class=\"configBox\">" +
                            "<div class=\"configTitle\">" + omg.templateName + " <input data-templateid=" + omg.templateId + "  value=\"0\"  name=\"selecteds\"  type=\"checkbox\" class=\"oneCheck\"/></div>";

                    }
			} else {
					str3 += "<div data-isavailable=" + omg.isAvailable + " data-isallocate=" + omg.isAllocate + "  class=\"configBoxs\">" +
						"<div class=\"configTitle\">" + omg.templateName + "</div>";
				}
				$.each(omg.imageInfo, function(key) {
					str3 += "<div class=\"imageName\">" + this + "</div>";
				});
				str3 += "</div>";
			});

		}

		var str4 = "</div>" +
			"<div class=\"popup_btn\">" +
			"<a class=\"popup_confirm exportBtn publicBtn hover-btn\">确定</a>" +
			"</div>" +
			"<div class=\"clear\"></div>" +
			"</div>" +
			"</div>";
		return str + str1 + str2 + str3 + str4;
	}



	var stuVirAllocation = {
		init: function() {
			this.bindEvent();
		},
		bindEvent: function() {
			var self = this;
			self.getDepartments();
			var json = {
				"pageNum": 1,
				"pageSize": 10
			};
			self.tabelList(json);
			$searchBtn.off("click").on("click", function() {
				var departmentId = $departNames.val();
				var jsonStr = {
					"pageNum": 1,
					"pageSize": 10
				};
				if(departmentId == 0) {
                    departmentId = "";
				} else {
					jsonStr.departmentId = Number(departmentId);
				}
				self.tabelList(jsonStr);
			});

		},
		virManaEvent: function() {
			//			虚机详情
			var self = this;
			$(".virManaBtn").off("click").click(function() {
				var json = JSON.stringify({
					"pageName":"stuVirrAllocationDetail",
					"paramData":{
						"classId":Number($(this).attr("data-classid"))
					}
				});
				window.location.href = "skipPage?jsonStr="+json;
			});
		},
		allocateVirEvent: function() {
			//			分配虚机
			var self = this;
			$(".virAllocateBtn").off("click").click(function() {
				$mb.show();
				var classid = $(this).attr("data-classid");
				$.ajax({
					type: "get",
					url: "server/allocate/class/" + classid,
					async: true,
					success: function(msg) {
						if(msg.code == 200) {
							if($(".popup").length != 0) {
								$(".popup").remove();
							} else {
								var box = virConfigBox(msg.data);
								$("body").append(box);
								$(".oneCheck").change(function() {
									if($(this).val() == "0") {
										$(this).val("1");
									} else {
										$(this).val("0");
									}
								});
								self.allocateVirSubmit(classid);
							}
						} else {
							Alert1.content = msg.message;
							Alert1.init();
							Alert1.show();
						}
					}
				});
			});
		},
		allocateVirSubmit: function(classid) {
			//			分配虚机提交及X
			var self = this;
			$(".exportBtn").off("click").click(function() {
					var arr = [];
					$.each($(".oneCheck[value=1]"), function(key) {
						arr.push(Number($(this).attr("data-templateid")));
					});
					var jsonStr = JSON.stringify(
						arr
					);
					loading1.show();
					$.ajax({
						type: "post",
						url: "server/allocate/class/" + classid,
                        contentType:"application/json;charset=utf-8",
						async: true,
						data: jsonStr,
						success: function(msg) {
							loading1.hide();
							if(msg.code == 200) {
								Alert1.content = "分配虚机成功";
								Alert1.success = function() {
									$(".popup").remove();
									$mb.hide();
									var json = {
										"pageNum": 1,
										"pageSize": 10
									};
									self.tabelList(json);
									$departNames.val(0);
								};
								Alert1.init();
								Alert1.show();
							} else {
								Alert1.content = msg.message;
								Alert1.init();
								Alert1.show();
							}
						}
					});

			});
			$(".popup_img").off("click").click(function() {
				$(".popup").remove();
				$mb.hide();
			});
		},
		getDepartments: function() {
			//			获取院系
			$.ajax({
				type: "post",
				url: "getDepartmentListPost",
				async: true,
				success: function(msg) {
					msg = JSON.parse(msg);
					if(msg.result == "success"){
                        if(msg.departmentList.length != 0) {
                            $.map(msg.departmentList, function(opt) {
                                $departNames.append("<option value=" + opt.departmentId + ">" + opt.departmentName + "</option>");
                            });
                        }
					}

				}
			});
		},
		tabelList: function(jsons) {
			var self = this;
			// json = JSON.stringify(jsons);
			loading1.show();
			$.ajax({
				type: "get",
				url: "server/allocate/class",
                contentType:"application/json;charset=utf-8",
				data: jsons,
				async: true,
				success: function(msg) {
					loading1.hide();
					if(msg.code == 200) {
						$tableBox.empty();
						if(msg.data.list.length != 0) {
							//列表
							var tabel = "";
							var startRow = msg.data.startRow;
							$.map(msg.data.list, function(opt) {
								tabel += tabelModel(opt, startRow);
								startRow++;
							});
							$tableBox.append(th + tabel);
							self.allocateVirEvent();
							self.virManaEvent();

						} else {
							var trImg = "<tr><td colspan='6'><img style='position:relative;margin: 80px auto 20px;' src='pc/images/ico_noneData.png'><p style='font-size:26px;color:#8a8a8a;width:100%;text-align:center;'>暂无数据</p></td></tr>";
							$tableBox.append(th + trImg)
						}
                        var sumpage = msg.data.pages;
                        if(sumpage == 0){
                            sumpage = 1;
                        }
                        var nowpage = 1;
                        var pagenumber = 10;
                        $(".tcdPageCode").createPage({
                            pageCount: sumpage, //总页数
                            current: nowpage, //当前页数
                            pageNumber: pagenumber, //每页显示数量
                            backFn: function(p) {
                                nowpage = p;
                                if(nowpage >= sumpage) {
                                    nowpage = sumpage;
                                }
                                var jsonStr = jsons;
                                jsonStr.pageNum = nowpage;
                                loading1.show();
                                $.ajax({
                                    type: "get",
                                    url: "server/allocate/class",
                                    contentType:"application/json;charset=utf-8",
                                    data: jsonStr,
                                    async: true,
                                    success: function(msg) {
                                        loading1.hide();
                                        //列表
                                        $tableBox.empty();
                                        var tabel = "";
                                        var startRow = msg.data.startRow;
                                        $.map(msg.data.list, function(opt) {
                                            tabel += tabelModel(opt, startRow);
                                            startRow++;
                                        });
                                        $tableBox.append(th + tabel);
                                        self.allocateVirEvent();
                                        self.virManaEvent();
                                    }
                                });
                            }
                        });
					} else {
						Alert1.content = msg.message;
						Alert1.init();
						Alert1.show();
					}

				}
			});

		}
	};
	stuVirAllocation.init();
});
document.onkeydown = function(e) {
    var ev = document.all ? window.event : e;
    if(ev.keyCode == 13) {
        return false;
    }
};